# -*- coding:utf-8 -*-

import pymongo
from qiniu import Auth, put_stream, BucketManager, put_data
import uuid
import requests
import io
import json
import datetime
import time
import queue
import os
import mimetypes
from threading import Thread

qiniu_access_key = 'd9wpWqKJVbyfS33ldbnEE4Ul3LEKUUIhAAhmHIkH'
qiniu_secret_key = 'h3GL6CZAYjtylEnBvi-91-3YHlvwZCPKt-zWO4Qt'
qiniu_bucket_name = 'daanjun'

WORKER_NUM = 24
global_queue = queue.Queue(500)

q = Auth(qiniu_access_key, qiniu_secret_key)
client = pymongo.MongoClient(host='localhost', port=27017)
db = client.superanswer


def get_file_type(name):
    name = name.lower()
    if len(name) <= 0:
        return ''

    filename, ext = os.path.splitext(name)
    return ext
    # elif name.endswith('.txt'):
    #     return '.txt'
    # elif name.endswith('.jpg'):
    #     return '.jpg'
    # elif name.endswith('.jpeg'):
    #     return '.jpeg'
    # elif name.endswith('.png'):
    #     return '.png'
    # elif name.endswith('.gif'):
    #     return '.gif'
    # elif name.endswith('.pdf'):
    #     return '.pdf'
    # elif name.endswith('.bmp'):
    #     return '.bmp'
    # elif name.endswith('.doc'):
    #     return '.doc'
    # elif name.endswith('.docx'):
    #     return '.docx'
    # elif name.endswith('.mp4'):
    #     return '.mp4'
    # elif name.endswith('.mpeg4'):
    #     return '.mpeg4'
    # elif name.endswith('.mpeg4'):
    #     return '.mpeg4'
    #
    # return ''


def upload_to_qiniu(aliyun_file_url):
    if type(aliyun_file_url) is not str:
        return ''
    print('url: {}'.format(aliyun_file_url))
    if aliyun_file_url.startswith('https://daan.dreamcapsule.top/'):
        return aliyun_file_url
        # bucket = BucketManager(q)
        # origin_name = aliyun_file_url[len('https://daan.dreamcapsule.top/'):]
        # if str(datetime.date.today()) in origin_name:
        #     return aliyun_file_url
        # new_name = str(datetime.date.today()) + '-' + origin_name
        # bucket.move(qiniu_bucket_name, origin_name, qiniu_bucket_name, new_name)
        # return 'https://daan.dreamcapsule.top/' + new_name
    elif 'aliyuncs.com' not in aliyun_file_url:
        return ''

    try:
        resp = requests.get(aliyun_file_url, headers={
            'referer': 'https://chaojidaan.cn/'
        })
        file_type = get_file_type(aliyun_file_url)
        filename = str(datetime.date.today()) + "-" + str(uuid.uuid4()) + file_type

        mime_type = mimetypes.MimeTypes().guess_type(aliyun_file_url)[0]
        if mime_type is None:
            mime_type = ''

        token = q.upload_token(qiniu_bucket_name, filename, 3600)
        if mime_type == '':
            with io.BytesIO(resp.content) as file:
                ret, info = put_stream(up_token=token, key=filename, input_stream=file, file_name=filename,
                                       data_size=len(resp.content))
                print('stream info: {}'.format(info))
                print('stream ret: {}'.format(ret))
                if ret is None:
                    return ''
                return 'https://daan.dreamcapsule.top/' + ret['key']

        ret, info = put_data(up_token=token, key=filename, data=resp.content, mime_type=mime_type)
        print('data info: {}'.format(info))
        print('data ret: {}'.format(ret))
        if ret is None:
            resp.close()
            del resp
            return ''

        resp.close()
        del resp
        return 'https://daan.dreamcapsule.top/' + ret['key']
    except requests.ConnectionError as e:
        print(e)
        return ''


def update_books():
    for item in db.books.find():
        if 'cover' in item and len(item['cover']) > 0:
            ret_url = upload_to_qiniu(item['cover'])
            if ret_url == '':
                continue
            item['cover'] = ret_url
            db.books.update({'_id': item['_id']}, item)


def update_bookcontributes():
    for item in db.bookcontributes.find():
        if 'cover' in item and len(item['cover']) > 0:
            ret_url = upload_to_qiniu(item['cover'])
            if ret_url == '':
                continue
            item['cover'] = ret_url
            db.bookcontributes.update({'_id': item['_id']}, item)
        if 'fileurl' in item and len(item['fileurl']) > 0:
            ret_url = upload_to_qiniu(item['fileurl'])
            if ret_url == '':
                continue
            item['fileurl'] = ret_url
            db.bookcontributes.update({'_id': item['_id']}, item)


def update_bookdocs_func():
    while True:
        item = global_queue.get()
        ret_url = upload_to_qiniu(item['url'])
        if ret_url == '':
            global_queue.task_done()
            continue
        item['url'] = ret_url
        db.bookdocs.update({'_id': item['_id']}, item)
        global_queue.task_done()


def update_bookdocs():
    for i in range(WORKER_NUM):
        worker = Thread(target=update_bookdocs_func)
        worker.setDaemon(True)
        worker.start()

    for item in db.bookdocs.find({'url': {'$regex': '.*aliyun.*'}}):
        if 'url' in item and len(item['url']) > 0:
            global_queue.put(item)

    global_queue.join()


def update_bookdocimages_func():
    while True:
        item = global_queue.get()
        ret_url = upload_to_qiniu(item['url'])
        if ret_url == '':
            global_queue.task_done()
            continue
        item['url'] = ret_url
        db.bookdocimages.update({'_id': item['_id']}, item)
        global_queue.task_done()


def update_bookdocimages():
    for i in range(WORKER_NUM):
        worker = Thread(target=update_bookdocimages_func)
        worker.setDaemon(True)
        worker.start()

    for item in db.bookdocimages.find({'url': {'$regex': '.*aliyun.*'}}):
        if 'url' in item and len(item['url']) > 0:
            global_queue.put(item)

    global_queue.join()


def update_categories():
    for item in db.categories.find():
        if 'icon' in item and len(item['icon']) > 0:
            ret_url = upload_to_qiniu(item['icon'])
            if ret_url == '':
                continue
            item['icon'] = ret_url
            db.categories.update({'_id': item['_id']}, item)


def update_cources():
    for item in db.cources.find():
        if 'image' in item and len(item['image']) > 0:
            ret_url = upload_to_qiniu(item['image'])
            if ret_url == '':
                continue
            item['image'] = ret_url
            db.cources.update({'_id': item['_id']}, item)

        if 'introImg' in item and len(item['introImg']) > 0:
            ret_url = upload_to_qiniu(item['introImg'])
            if ret_url == '':
                continue
            item['introImg'] = ret_url
            db.cources.update({'_id': item['_id']}, item)


def update_discusses():
    for item in db.discusses.find():
        if 'cover' in item and len(item['cover']) > 0:
            lst = list(json.loads(item['cover']))
            if len(lst) <= 0:
                continue

            new_lst = []
            for key in lst:
                ret_url = upload_to_qiniu(key)
                if ret_url == '':
                    continue
                new_lst.append(ret_url)

            item['cover'] = json.dumps(new_lst)
            db.discusses.update({'_id': item['_id']}, item)


def update_imagealerts():
    for item in db.imagealerts.find():
        if 'imageUrl' in item and len(item['imageUrl']) > 0:
            ret_url = upload_to_qiniu(item['imageUrl'])
            if ret_url == '':
                continue
            item['imageUrl'] = ret_url
            db.imagealerts.update({'_id': item['_id']}, item)


def update_inviteactivities():
    for item in db.inviteactivities.find():
        if 'headImageUrl' in item and len(item['headImageUrl']) > 0:
            ret_url = upload_to_qiniu(item['headImageUrl'])
            if ret_url == '':
                continue
            item['headImageUrl'] = ret_url
            db.inviteactivities.update({'_id': item['_id']}, item)

        if 'shareImageUrl' in item and len(item['shareImageUrl']) > 0:
            ret_url = upload_to_qiniu(item['shareImageUrl'])
            if ret_url == '':
                continue
            item['shareImageUrl'] = ret_url
            db.inviteactivities.update({'_id': item['_id']}, item)

        if 'detailImages' in item and len(item['detailImages']) > 0:
            ret_url = upload_to_qiniu(item['detailImages'])
            if ret_url == '':
                continue
            item['detailImages'] = ret_url
            db.inviteactivities.update({'_id': item['_id']}, item)


def handle_scbooks_func():
    while True:
        item = global_queue.get()
        ret_url = upload_to_qiniu(item['pic'])
        if ret_url == '':
            continue
        item['pic'] = ret_url
        db.scbooks.update({'_id': item['_id']}, item)
        global_queue.task_done()


def update_scbooks():
    for i in range(WORKER_NUM):
        worker = Thread(target=handle_scbooks_func)
        worker.setDaemon(True)
        worker.start()

    for item in db.scbooks.find():
        if 'pic' in item and len(item['pic']) > 0:
            global_queue.put(item)

    global_queue.join()


def update_schools():
    for item in db.schools.find():
        if 'logo' in item and len(item['logo']) > 0:
            ret_url = upload_to_qiniu(item['logo'])
            if ret_url == '':
                continue
            item['logo'] = ret_url
            db.schools.update({'_id': item['_id']}, item)


def update_cource_chapters():
    for item in db.courcechapters.find():
        if 'url' in item and len(item['url']) > 0:
            ret_url = upload_to_qiniu(item['url'])
            if ret_url == '':
                continue
            item['url'] = ret_url
            db.courcechapters.update({'_id': item['_id']}, item)


def update_settings():
    for item in db.settings.find():
        if 'value' in item and len(item['value']) > 0:
            lst = list(item['value'].split(' '))
            if len(lst) <= 0:
                continue

            new_lst = []
            for key in lst:
                ret_url = upload_to_qiniu(key)
                if ret_url == '':
                    continue
                new_lst.append(ret_url)

            item['value'] = ' '.join(new_lst)
            ret_url = upload_to_qiniu(item['value'])
            if ret_url == '':
                continue
            item['url'] = ret_url
            db.settings.update({'_id': item['_id']}, item)


def update_videos():
    for item in db.videos.find():
        if 'src' in item and len(item['src']) > 0:
            ret_url = upload_to_qiniu(item['src'])
            if ret_url == '':
                continue
            item['src'] = ret_url
            db.videos.update({'_id': item['_id']}, item)


# books['cover']
# bookcontributes['cover']
# bookcontributes['fileurl']
# bookdocs['url']
# bookdocimages['url']
# categories['icon']
# cources['image']
# cources['introImage']
# discusses['cover']
# imagealerts['imageUrl']
# inviteactivities['headImageUrl']
# inviteactivities['detailImages'] list
# scbooks['pic']
# schools['logo']

if __name__ == '__main__':
    # update_videos()
    # update_settings()
    update_cource_chapters()
    # upload_to_qiniu('https://cjdaoss.oss-cn-beijing.aliyuncs.com/kyzykzt-doc-pdfs/a07c9fc8-f018-11e8-9d8e-8c8590a3c4f1.jpg')
    # update_discusses()
    # update_inviteactivities()
    # update_bookcontributes()
    # update_imagealerts()
    # update_categories()
    # update_cources()
    # update_schools()
    # update_scbooks()
    # update_books()
    # update_bookdocs()
    # update_bookdocimages()
